﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class Toss : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    TossBetting tossBetting;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Client"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatch();
            }
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheForClientResult> lstResult = new List<GetTodayMatcheForClientResult>();
        lstResult = matchSchedule.GetTodayMatchesForClient();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMatch.SelectedItem.Text.ToLower() != "select match")
        {
            matchSchedule = new MatchSchedule(_dataObject);
            List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
            lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpMatch.SelectedValue));
            if (lstResult.Count > 0)
            {
                drpTeam.DataSource = lstResult;
                drpTeam.DataTextField = "TeamName";
                drpTeam.DataValueField = "Id";
                drpTeam.DataBind();
                drpTeam.Items.Insert(0, new ListItem("Select Team", "0"));


            }
        }
    }
    [WebMethod]
    public static List<GetActiveMatchByIdResult> GetActiveMatch(int matchId)
    {
        IDataRepository _dataObject1 = new DataRepository();
        AdminToss adminToss;
        adminToss = new AdminToss(_dataObject1);
        List<GetActiveMatchByIdResult> lstResult = new List<GetActiveMatchByIdResult>();
        lstResult = adminToss.GetActiveMatchById(matchId);
        if (lstResult != null && lstResult.Count > 0)
            return lstResult;
        else
            return null;
    }
    protected void backBetting_Click(object sender, EventArgs e)
    {
        Response.Redirect("Betting.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        tossBetting = new TossBetting(_dataObject);
        HttpCookie cookie = Request.Cookies["Client"];
        tossBetting.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
        tossBetting.ClientId = Convert.ToInt32(cookie["ClientId"]);
        tossBetting.TeamId = Convert.ToInt32(drpTeam.SelectedValue);
        tossBetting.Rate = txtAmount.Text;
        tossBetting.CreatedDate = System.DateTime.Now;
        tossBetting.Create();
        layerSuccess.InnerHtml = "<strong>Error!</strong> Record added sucessfully."; ;
        layerSuccess.Attributes.Add("style", "display:block;");
        layerError.Attributes.Add("style", "display:none;");
        clearControl();

    }

    private void clearControl()
    {
        drpMatch.SelectedIndex = -1;
        drpTeam.SelectedIndex = -1;
        txtAmount.Text = string.Empty;
    }
}